//
//  SocialUtils_ios.mm
//  WXDemo
//
//  Created by Joueu on 14-11-6.
//
//

#include "SocialUtils.h"

#import "WXApi.h"

static WXScene _scene = WXSceneTimeline; // 发送到微信朋友圈
//static WXScene _scene = WXSceneSession;
static UIViewController* m_viewController;

// 分享的内容，包括：应用名、标题、描述、链接
static NSString* SHARE_APP_NAME;
static NSString* SHARE_CAPTION;
static NSString* SHARE_DESCRIPTION;
static NSString* SHARE_LINK = @"HelKyle.tk";

// 获取设备语言环境
const char* SocialUtils::getDeviceLanguage(){
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    NSArray * allLanguages = [defaults objectForKey:@"AppleLanguages"];
    NSString * preferredLang = [allLanguages objectAtIndex:0];
    return [preferredLang UTF8String];
}

void SocialUtils::setShareContent(){
    NSString* lan = [NSString stringWithUTF8String:getDeviceLanguage()];
    if ([lan isEqualToString:@"zh-Hans"]) { // 如果是简体中文
        SHARE_APP_NAME = @"快来领养属于你的植物吧~";
        SHARE_CAPTION = @"快来领养属于你的植物吧~";
        SHARE_DESCRIPTION = @"快来领养属于你的植物吧~";
//    }else if ([lan isEqualToString:@"zh-Hant"]) { // 如果是繁体中文
//        SHARE_APP_NAME = @"測試分享功能的APP";
//        SHARE_CAPTION = @"測試分享功能的APP";
//        SHARE_DESCRIPTION = @"我是用來測試的！";
    }else { // 默认是英语
        SHARE_APP_NAME = @"This Palnt is Cute";
        SHARE_CAPTION = @"This Palnt is Cute";
        SHARE_DESCRIPTION = @"This Palnt is Cute!";
    }
}

//void SocialUtils::setUIViewController(void* viewController){
//    m_viewController = (UIViewController*)viewController;
//}

// 注册微信
void SocialUtils::registerWeixinForIOS(char *appID){
    [WXApi registerApp:[NSString stringWithUTF8String:appID] withDescription:nil];
}

 void SocialUtils::setUIViewController(void* viewController){
    m_viewController = (UIViewController*)viewController;
    // 设置分享内容（国际化）
    setShareContent();
}


//// 注册微信
//void SocialUtils::registerWeixinForIOS(char *appID){
//    [WXApi registerApp:[NSString stringWithUTF8String:appID] withDescription:nil];
//}

void SocialUtils::shareToWeixinForIOS(){
    // 设置分享内容
    setShareContent();
    
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = SHARE_DESCRIPTION;
    message.description = SHARE_DESCRIPTION;
    [message setThumbImage:[UIImage imageNamed:@"Icon-76@2x.png"]];
    
    WXWebpageObject *ext = [WXWebpageObject object];
    ext.webpageUrl = SHARE_LINK;
    
    message.mediaObject = ext;
    
    SendMessageToWXReq* req = [[[SendMessageToWXReq alloc] init]autorelease];
    req.bText = NO;
    req.message = message;
    req.scene = _scene;
    
    [WXApi sendReq:req];
}

void SocialUtils::setShareType(int type)
{
    if (type == 0) {
        _scene = WXSceneTimeline;
    }else{
        _scene = WXSceneSession;
    }
}